package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.jframe.cfg.model.DataSrc;
import com.jframe.cfg.model.Iframe;
import com.jframe.cfg.model.RoleDatasrc;
import com.jframe.cfg.model.Rolemenu;
import com.jframe.cfg.model.Sysmenu;
import com.jframe.cfg.service.DatasrcService;
import com.jframe.cfg.service.IframeService;
import com.jframe.cfg.service.RoledataSrcService;
import com.jframe.cfg.service.RolemenuService;
import com.jframe.cfg.service.SysmenuService;
import com.jframe.sso.model.Role;
import com.jframe.sso.service.RoleService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.TabmCache;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Tree;

public class RoleDataAction extends ExtendAction {
	
	public void getRightTrees(){
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		RoleService roleService = (RoleService)SpringUtil.getBean("roleService");
		List<Role> treels = roleService.findByGroupId(groupId);
		List<Tree> ls = new ArrayList<Tree>();
		for(Role role:treels){
			Tree tree = new Tree();
			tree.setId(""+role.getId());
			tree.setText(role.getName());						
			ls.add(tree);
		}
		this.print(JsonUtil.list2Json(ls));
	}
	
	public void Iframelist(){
		int roleId = StringUtil.parseInt(request.getParameter("roleId"));
		//RolefunService rolefunService = (RolefunService) this.getBean("rolefunService");
		RolemenuService rolemenuService = (RolemenuService)this.getBean("rolemenuService");
		IframeService iframeService = (IframeService)this.getBean("iframeService");
		//获取该角色下的所有菜单
		Rolemenu rolemenu = rolemenuService.load("roleId", roleId);		
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> sysmenus = sysmenuService.findListByIds(rolemenu.getMenuIds());
		//获取该菜单下的全部叶签,
		List<Iframe> iframes = iframeService.findListByMenuCodes(this.getMenuCodes(sysmenus));
		//装载成ztree
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		for(Iframe iframe:iframes){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", "root_"+iframe.getId());
			map.put("name", iframe.getTitle());
			map.put("children", this.getChildrenData(""+iframe.getId()));
			items.add(map);
		}
		this.print(JsonUtil.list2Json(items));
	}
	
	public void getIframeList(){
		int roleId = StringUtil.parseInt(request.getParameter("roleId"));
		RoleService roleService = (RoleService)this.getBean("roleService");
		RolemenuService rolemenuService = (RolemenuService)this.getBean("rolemenuService");
		Role role = roleService.load(roleId);
		Rolemenu rolemenu = rolemenuService.load("roleId", roleId);
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> list = sysmenuService.findListByUpIdAndGroupId(1,role.getGroupId());
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		for(Sysmenu menu:list){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", "menu"+menu.getId());
			map.put("text", menu.getText());
			map.put("children", getIframeMaps(menu.getId(), rolemenu.getMenuIds()));
			items.add(map);
		}
		this.print(JsonUtil.list2Json(items));
	}
	
	private List<Map<String,Object>> getIframeMaps(int menuId,String rolemenuIds){
		IframeService iframeService = (IframeService)this.getBean("iframeService");
		List<Map<String,Object>> iframeMaps = iframeService.findListMapByproperties(menuId, rolemenuIds);
		for(Map<String,Object> map:iframeMaps){
			map.put("children", getChildrenData(map.get("id").toString()));
			map.put("id", "iframes"+map.get("id"));
		}
		return iframeMaps;
	}
	
	private List<Map<String,Object>> getChildrenData(String iframeId){
		List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
		DatasrcService dataSrcService = (DatasrcService)this.getBean("dataSrcService");
		List<DataSrc> list = dataSrcService.findListByIframeId(iframeId);
		for(DataSrc datasrc:list){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", datasrc.getId());
			map.put("text", datasrc.getName());
			items.add(map);
		}
		return items;
	}
	
	public void funlist(){
		int datasrcId = StringUtil.parseInt(request.getParameter("datasrcId"));
		int roleId = StringUtil.parseInt(this.request.getParameter("roleId"));
		String tabId = StringUtil.null2Space(this.request.getParameter("tabId"));
		DatasrcService dataSrcService = (DatasrcService)this.getBean("dataSrcService");		
		DataSrc datasrc = dataSrcService.load(datasrcId);
		RoledataSrcService roledataSrcService = (RoledataSrcService)this.getBean("roledataSrcService");
		RoleDatasrc roleDataSrc = roledataSrcService.getRoleDatasrcByProperties(roleId, datasrc.getIframeId(),tabId, datasrcId);
		String roleDataIds = StringUtil.null2Space(roleDataSrc.getDatasrcIds());		
		String json = TabmCache.getCombotreeJson(datasrc.getDatasrc());
		if(!roleDataIds.equals("")){
			json = JsonUtil.list2Json(this.jsonTree(json,roleDataIds));	
		}
		this.print(json);		
	}
	
	public void save(){
		int roleId = StringUtil.parseInt(this.request.getParameter("roleId"));
		String tabId = StringUtil.null2Space(this.request.getParameter("tabId"));
		int datasrcId = StringUtil.parseInt(this.request.getParameter("datasrcId"));
		String ids = StringUtil.null2Space(this.request.getParameter("ids"));
		DatasrcService dataSrcService = (DatasrcService)this.getBean("dataSrcService");
		DataSrc datasrc = dataSrcService.load(datasrcId);
		String iframeId = datasrc.getIframeId();
		RoledataSrcService roledataSrcService = (RoledataSrcService)this.getBean("roledataSrcService");
		RoleDatasrc roleDataSrc = roledataSrcService.getRoleDatasrcByProperties(roleId, iframeId,tabId, datasrcId);
		roleDataSrc.setDatasrcIds(ids);
		roleDataSrc.setIframeId(iframeId);
		roleDataSrc.setRoleId(roleId);
		roleDataSrc.setTabId(tabId);
		roleDataSrc.setDatasrcId(datasrcId);
		roledataSrcService.save(roleDataSrc);
		this.print(true);
	}
	
	private String getMenuCodes(List<Sysmenu> sysmenus){
		String codes = "";
		for(Sysmenu menu:sysmenus){
			codes +=menu.getCode()+",";
		}
		if(sysmenus.size()>0){
			codes = codes.substring(0,codes.length()-1);
		}
		return codes;
	}
	 
    /** 
     * 从json对象集合表达式中得到�?��java对象列表
     * @param jsonString
     * @param pojoClass
     * @return
     */
    private static List json2List(String jsonStr){
        if(jsonStr==null){
        	return new ArrayList();
        }else{
	        JSONArray jsonArray = JSONArray.fromObject(jsonStr);
	        JSONObject jsonObject;
	        Object obj;
	        List list = new ArrayList();
	        for ( int i = 0 ; i<jsonArray.size(); i++){           
	            jsonObject = jsonArray.getJSONObject(i);
	            obj = JSONObject.toBean(jsonObject);
	            list.add(obj);
	        }
	        return list;
        }
    }
    
    private List jsonTree(String jsonlist,String roleDataIds){
    	List maplist = new ArrayList();
    	List list = this.json2List(jsonlist);
		for(Object obj : list){
			String objjson = JsonUtil.obj2Json(obj);
			Map map = JsonUtil.json2Map(objjson);
			String children = StringUtil.null2Space(map.get("children"));
			if(!children.equals("")){
				map.put("children", this.jsonTree(children, roleDataIds));
			}
			if(isIn(roleDataIds,map.get("id").toString())){
				map.put("checked", true);
			}else{
				map.put("checked", false);
			}
			maplist.add(map);
		}
    	return maplist;
    }
    
    private boolean isIn(String roleDataIds,String roleDataId){
		boolean flag = false;
		String[] ids =  StringUtil.null2Space(roleDataIds).split(",");
		for(String id:ids){
			if(id.equals(roleDataId)){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
}
